let userList = [];
const app = require("express")();
const server = require("http").createServer(app);
const io = require("socket.io")(server);

io.on("connection", (socket) => {

    console.log("恭喜id:" + socket.id + "连接成功！");
    socket.on("joinChannel", (user) => {
        userList.push({ id: socket.id, username: user });
        console.log("当前用户列表:",userList)
        io.emit("allUser", userList);
    });
    socket.on("test", (data) => {
        console.log('123'+data)
    },)
    socket.on("disconnect",(reason) => {
        console.log(reason)
        let m = 0;
        for (let i = 0; i < userList.length; i++) {
            if (userList[i].id === socket.id){
                m = i ;
                break;
            }
        }
        userList.splice(m,1)
        console.log("当前用户列表:",userList)

        io.emit("allUser", userList);
    })


    socket.on("sendMessage",(chooseId,message) => {
        console.log(chooseId)
        console.log(message)
        socket.to(chooseId).emit("receiveMessage", socket.id, message);
    })

});


server.listen(12345, () => {
    console.log("服务监听12345启动.....已启动");
});
